﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using mmoss.configuration;
using mmoss.orm;
using mmoss.classes;
using mmoss.application.classes;

namespace mmoss.application
{
    public partial class frmContainer : Form
    {
        //todo
        //add these to config values
        private string ScanBuffer;
        private char ScanPrefix;
        private char ScanSuffic;

        public KeystrokeMessageFilter keyStrokeMessageFilter = new KeystrokeMessageFilter();

        public User LoggedInUser { get; set; }
        public frmContainer()
        {
            this.Load += new EventHandler(frmContainer_Load);
            InitializeComponent();
        }

        void frmContainer_Load(object sender, EventArgs e)
        {
            lblLocationText.Text = "";
            lblUserText.Text = "";

            //add the global keystroke filter for scanning cards
            Application.AddMessageFilter(keyStrokeMessageFilter);
            //set the menuing
            mnuNoLogin.Visible = true;
            mnuPostLogin.Visible = false;
            //handle keyup event
            keyStrokeMessageFilter.CCScanned += new KeystrokeMessageFilter.CCScannedGood(keyStrokeMessageFilter_CCScanned);
            keyStrokeMessageFilter.LicenseScanned += new KeystrokeMessageFilter.LicenseScannedGood(keyStrokeMessageFilter_LicenseScanned);
            //this.KeyUp+=new KeyEventHandler(frmContainer_KeyUp);

            //go full-screen
            this.WindowState = FormWindowState.Maximized;
            //launch login
            Login l_login = new Login();
            l_login.MdiParent = this;
            l_login.Show();
            l_login.FormClosed += new FormClosedEventHandler(l_login_FormClosed);
            
        }

        void keyStrokeMessageFilter_LicenseScanned(DriversLicense l_License)
        {
            //CO drivers license scanned - ask if we want to check in patient
            Patient l_test = dbConn.db.Patients.Get(l_License.CardNumber);
            if (l_test != null)
            {
                if (MessageBox.Show("Would you like to checkin patient " + l_test.patientLastName + ", " + l_test.patientFirstName, "checkin patient?", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    //checkin patient, make sure docked patient window shows.
                }
            }
        }

        void keyStrokeMessageFilter_CCScanned(CCCard l_Card)
        {
            //nothing for now
        }

        public void frmContainer_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 101)
            {
            }
        }

        void l_login_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (((Login)sender).UserSuccess != null)
            {
                //set the user
                LoggedInUser = ((Login)sender).UserSuccess;

                if (Default.CurrentUser.UserPermissionsXrefs.Select(p => p.Location).Distinct().Count() > 1)
                {
                    //more than one location - select location
                    frmChooseLocation l_choose = new frmChooseLocation();
                    l_choose.FormClosed += new FormClosedEventHandler(l_choose_FormClosed);
                    l_choose.ShowDialog();
                }
            }
        }

        void l_choose_FormClosed(object sender, FormClosedEventArgs e)
        {
            //selected form was closed - set location information and menu information
            Location l_selectedLocation = ((frmChooseLocation)sender).SelectedLocation;
            switch (l_selectedLocation.locationTypeID)
            {
                case 1:
                    //mmc
                    mnuGrow.Visible=false;
                    mnuNoLogin.Visible=false;
                    mnuPostLogin.Visible=true;
                    Default.CurrentLocation=l_selectedLocation;
                    //launch patient dock
                    //launch the patient screen
                    frmDockPatients l_win = new frmDockPatients();
                    l_win.MdiParent = this;
                    l_win.Dock = DockStyle.Right;
                    l_win.Show();

                    break;
                case 2:
                    //opc
                    mnuGrow.Visible=true;
                    mnuNoLogin.Visible=false;
                    mnuPostLogin.Visible=false;
                    Default.CurrentLocation=l_selectedLocation;
                    //todo - launch plant management
                    break;
                case 3:
                    //mips
                    break;
            }
            lblLocationText.Text = "Location: " + Default.CurrentLocation.locationName;
            lblUserText.Text = "User: " + Default.CurrentUser.userName;

        }

        public void updatePatientList()
        {
            foreach (Form l_item in MdiChildren)
            {
                if (l_item.GetType().ToString().Contains("frmDockPatients"))
                {
                    ((frmDockPatients)l_item).UpdateList();
                }
            }
        }
        public void launchSale(PatientCheckIn l_patient)
        {
            frmSale l_win = new frmSale(l_patient.Patient);
            l_win.CurrentCheckin = l_patient;
            l_win.MdiParent = this;
            l_win.Dock = DockStyle.Left;
            l_win.Show();
        }
        private void patientsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmPatients l_patient = new frmPatients();
            l_patient.MdiParent = this;
            l_patient.Show();
        }

        private void salesToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void itemsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmAdminInventoryItem l_items = new frmAdminInventoryItem();
            l_items.MdiParent = this;
            l_items.Show();
        }

        private void mMCItemsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmAdminInventoryItemVendor l_items = new frmAdminInventoryItemVendor();
            l_items.MdiParent = this;
            l_items.Show();
        }

        private void receiveItemsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmSale l_sale = new frmSale();
            l_sale.MdiParent = this;
            l_sale.Dock = DockStyle.Left;
            l_sale.Show();
        }

        private void setTouchscreenOnToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Default.TouchScreen = (((ToolStripItem)sender).Text == "Enable TouchScreen");
            ((ToolStripItem)sender).Text = (((ToolStripItem)sender).Text == "Disable TouchScreen" ? "Enable TouchScreen" : "Disable TouchScreen");
        }

        private void aboutToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            frmAbout l_about = new frmAbout();
            l_about.Show();
        }

        private void aboutMMOSSToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmAbout l_about = new frmAbout();
            l_about.Show();
        }

        private void vendorsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmVendor l_vendor = new frmVendor();
            l_vendor.MdiParent = this;
            l_vendor.Dock = DockStyle.Left;
            l_vendor.Show();
        }

        private void tagsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmGrowTags l_grow = new frmGrowTags();
            l_grow.Dock = DockStyle.Left;
            l_grow.MdiParent = this;
            l_grow.Show();
        }


        private void updateCoordinatesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Patient l_patient in dbConn.db.Patients)
            {
                if (l_patient.patientAddress!=null && l_patient.patientAddress.Trim() != "" && l_patient.patientCity!=null &&  l_patient.patientCity != "" && l_patient.patientState!=null &&  l_patient.patientState != "")
                {
                    mmoss.classes.Mapping.loadLatLong(l_patient);
                    dbConn.db.SubmitChanges();
                }
            }
        }

        private void updateDistancesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Patient l_patient in dbConn.db.Patients)
            {
                if (l_patient.patientAddressLat!=null)
                {
                    foreach (Location l_mmc in dbConn.db.Locations.Where(p=>p.LocationType.locationTypeID==1).ToList())
                    {
                        if (dbConn.db.PatientMMCDistanceXfers.Where(p => p.patientID == l_patient.patientID
                            && p.mmcID == l_mmc.Id).ToList().Count == 0)
                        {
                            PatientMMCDistanceXfer l_distance = new PatientMMCDistanceXfer();
                            l_distance.locationID= l_mmc.Id;
                            l_distance.patientID = l_patient.patientID;
                            l_distance.distanceInMiles = decimal.Parse(Math.Round(mmoss.classes.Mapping.distance
                                (double.Parse(l_patient.patientAddressLat.ToString())
                                , double.Parse(l_patient.patientAddressLong.ToString())
                                , double.Parse(l_mmc.locationLat.ToString())
                                , double.Parse(l_mmc.locationLong.ToString())
                                ,'M'
                                ),3).ToString());
                            dbConn.db.PatientMMCDistanceXfers.InsertOnSubmit(l_distance);
                            dbConn.db.SubmitChanges();
                        }
                    }
                }
            }

        }

        private void test1ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmViewReport l_report = new frmViewReport();
            l_report.MdiParent = this;
            l_report.Show();
            
        }

        private void loginToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Login l_login = new Login();
            l_login.MdiParent = this;
            l_login.Show();
            l_login.FormClosed += new FormClosedEventHandler(l_login_FormClosed);
        }

        private void receiveToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void physicalInventoryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Inventory l_inventory = new Inventory();
            l_inventory.MdiParent = this;
            l_inventory.Show();
        }

        private void logoutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //close all windows
            foreach (Form l_form in this.MdiChildren)
            {
                l_form.Close();
            }
            //change menu
            mnuGrow.Visible = false;
            mnuPostLogin.Visible = false;
            mnuNoLogin.Visible = true;
            //bring up login
            Login l_login = new Login();
            l_login.MdiParent = this;
            l_login.Show();
            l_login.FormClosed += new FormClosedEventHandler(l_login_FormClosed);
        }

        private void changeLocationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //close the locations
            foreach (Form l_form in this.MdiChildren)
            {
                l_form.Close();
            }
            frmChooseLocation l_choose = new frmChooseLocation();
            l_choose.FormClosed += new FormClosedEventHandler(l_choose_FormClosed);
            l_choose.ShowDialog();
        }



    }
}
